//
//  NgListViewController.m
//  NgTopOSShove
//
//  Created by yscompany on 2018/11/20.
//  Copyright © 2018 NgTopOS. All rights reserved.
//

#import "NgListViewController.h"
#import "Shop.h"
#import "WaterFlowLayout.h"
#import "NgShopCollectionViewCell.h"


@interface NgListViewController ()<WaterFlowLayoutDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) NSMutableArray *shops;
@end

@implementation NgListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.baseCollectionView];
    
    [self setupRefresh];
    // Do any additional setup after loading the view.
}

- (void)setupRefresh
{
    self.baseCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewShops)];
    [self.baseCollectionView.mj_header beginRefreshing];
    
    self.baseCollectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreShops)];
    self.baseCollectionView.mj_footer.hidden = YES;
}

- (void)loadNewShops
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSMutableArray *shops = [Shop mj_objectArrayWithFilename:@"1.plist"];
        [self.shops removeAllObjects];
        [self.shops addObjectsFromArray:shops];
        
        [self.baseCollectionView reloadData];
        
        [self.baseCollectionView.mj_header endRefreshing];
    });
}

- (void)loadMoreShops
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSMutableArray *shops = [Shop mj_objectArrayWithFilename:@"1.plist"];
        [self.shops addObjectsFromArray:shops];
        
        // 刷新数据
        [self.baseCollectionView reloadData];
        
        [self.baseCollectionView.mj_footer endRefreshing];
    });
}
#pragma mark - collectionview delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    self.baseCollectionView.mj_footer.hidden = self.shops.count == 0;
    return self.shops.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NgShopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([NgShopCollectionViewCell class]) forIndexPath:indexPath];
    if (self.shops.count > indexPath.row) {
        [cell configureShop:[self.shops objectAtIndex:indexPath.row]];
    }
    return cell;
 
}
- (CGFloat)WaterFlowLayout:(WaterFlowLayout *)WaterFlowLayout heightForRowAtIndexPath:(NSInteger )index itemWidth:(CGFloat)itemWidth
{
    Shop *shop = self.shops[index];
    
    return itemWidth * shop.h / shop.w;
}


#pragma mark - getter method
- (UICollectionView *)baseCollectionView {
    if (!_baseCollectionView) {
        WaterFlowLayout *layout = [[WaterFlowLayout alloc]init];
        layout.delegate = self;
        _baseCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _baseCollectionView.showsHorizontalScrollIndicator = NO;
        _baseCollectionView.showsVerticalScrollIndicator = NO;
        _baseCollectionView.backgroundColor = [UIColor whiteColor];
        _baseCollectionView.dataSource = self;
        _baseCollectionView.delegate = self;
        [_baseCollectionView registerClass:[NgShopCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([NgShopCollectionViewCell class])];
    }
    return _baseCollectionView;
}
- (NSMutableArray *)shops
{
    if (!_shops) {
        _shops = [NSMutableArray array];
    }
    return _shops;
}

@end
